<template>
  <div class="AddOrUpdate_main add_container tenantMenu_main main_container user_container">
    <div v-loading="loading" class="main_main">
      <div class="main_main_container">
        <div class="main_main_left">
          <div class="title" />
          <!-- <el-input
            v-model="filterText"
            clearable
            style="margin-bottom:5px"
            placeholder="请输入设备类型"
          /> -->
          <div class="main_main_search_input">
            <el-input
              v-model="filterText"
              style="width:195px"
              clearable
              placeholder="请输入设备类型"
              @keyup.enter.native="secrchTreeNode"
              @clear="secrchTreeNode"
            />
            <el-button style="background:#387bfc;width:30px;" icon="el-icon-search" @click.stop="secrchTreeNode" />
          </div>
          <div class="main_main_left_top">
            <el-tree
              ref="tree"
              node-key="id"
              :current-node-key="typeId"
              :data="typeListArr"
              :expand-on-click-node="false"
              :default-expand-all="true"
              :highlight-current="true"
              :props="defaultProps"
              :filter-node-method="filterNode"
              @node-click="getEquimentInfo"
            />
          </div>
        </div>
        <div class="main_main_right">
          <div style="padding-right:20px" class="SearchBar_main main_main_search">
            <div style="padding-left:0" class="main_main_search_item">
              <div class="formItem_header">
                <div class="formItem_header_operate">
                  <a class="formItem_header_operate_items" @click="add">
                    <svg-icon icon-class="新增" />
                    <div class="item_name">新增</div>
                  </a>
                </div>
              </div>
            </div>
            <div class="main_main_search_item">
              <el-input v-model="pageData.searchValue" placeholder="请输入设备编码或名称" clearable @keyup.enter.native="getList" @clear="getList" />
              <el-button icon="el-icon-search" @click.stop="getList" />
            </div>
          </div>
          <div ref="table_container" class="table_container">
            <el-table
              ref="table"
              :data="tableData"
              :row-class-name="tableRowClass"
              :height="tableHeight"
              style="width: 99.9%"
              stripe
              border
              row-key="menuId"
              @sort-change="(column)=>{sortChange(column, this)}"
              @row-click="singleElection"
            >
              <Column
                v-for="(column, index) in defaultTableColumns"
                :key="index"
                :prop="column.prop"
                :is-show="column.isShow"
                :label="column.label"
                :sortable="column.sortable"
                :width="column.width"
              />
            </el-table>
          </div>

          <div class="item_list">
            <pagination
              :total="total"
              :page.sync="pageData.pageNum"
              :limit.sync="pageData.pageSize"
              :selected-num="selectionsAll&&selectionsAll.length"
              @pagination="getList"
            />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { list, remove, listType } from '@/api/equipmentManage/baseInfo/equipmentInfo'
import { list as typeList } from '@/api/equipmentManage/baseInfo/equipmentType'
import AddOrUpdate from './addOrUpdate' // 新增或编辑页面

export default {
  name: 'Index',
  props: {
    layerid: {},
    bookId: {}
  },
  data() {
    return {
      loading: true,
      filterText: '', // 树节点搜索字段
      pageId: '设备信息', // 页面唯一标识
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc',
        status: 1
      },
      typeData: {
        status: 1
      },
      typeListArr: [],
      userId: '',
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'code', label: '设备编码' },
        { prop: 'name', label: '设备名称' },
        { prop: 'typeName', label: '设备类型' },
        { prop: 'location', label: '设备位置' },
        { prop: 'model', label: '规格型号' },
        { prop: 'firmName', label: '设备厂商' },
        { prop: 'purchaseTime', label: '购买日期' },
        { prop: 'warrantyEndTime', label: '保修截止日期' },
        { prop: 'deptName', label: '使用部门' },
        { prop: 'realName', label: '设备负责人' },
        { prop: 'machineNumber', label: '机号台' },
        { prop: 'protoName', label: '通讯协议' },
        { prop: 'ip', label: 'IP' },
        { prop: 'port', label: '端口' },
        { prop: 'remark', label: '备注' }
      ],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'code', label: '设备编码', type: 'input' },
        { prop: 'name', label: '设备名称', type: 'input' },
        { prop: 'typeId', label: '设备类型', type: 'select', optionsName: 'equipmentInfoOptions' },
        { prop: 'model', label: '规格型号', type: 'input' }
      ],

      operationData: [ // 右上角的操作按钮
        { label: '新增', perm: '' },
        { label: '导入', perm: '' },
        { label: '条码打印', perm: '' },
        { label: '批量操作', perm: '' },
        { label: '批量删除', perm: '' },
        { label: '批量导出', perm: '' },
        { label: '页面配置', perm: '' }
      ],

      equipmentInfoOptions: [],
      templateSelection: undefined,
      selectionsRow: {},
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      rowId: '',
      typeId: ''
    }
  },
  // watch: {
  //   filterText(val) {
  //   }
  // },
  mounted() {
    // this.defaultSearchColumns[1].options = this.statusOptions // 给 defaultSearchColumns 赋值
    this.getList()
    this.getTypeList()
    this.$nextTick(() => {
      this.tableHeight = this.$refs.table_container.offsetHeight
    })
    this.templateSelection = this.bookId
    this.userId = this.bookId
  },
  methods: {
    secrchTreeNode() {
      this.$refs.tree.filter(this.filterText)
    },
    // 树节点搜索
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    getEquimentInfo(item) {
      console.log(item)
      this.typeId = item.id
      this.getList()
    },
    getTypeList() {
      typeList(this.pageData).then(res => {
        this.typeListArr.push({ name: '设备类型', id: '', children: [] })
        this.typeListArr[0].children = res.data
        this.getEquimentInfo({ name: '设备类型', id: '', children: [] })
      })
    },
    getList() {
      list({ ...this.pageData, typeId: this.typeId, status: 1 }).then(res => {
        this.tableData = res.data
        this.total = res.total
        this.loading = false
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
        })
      })
      listType().then(res => {
        res.data.forEach(item => {
          this.equipmentInfoOptions.push({
            value: item.id,
            label: item.name
          })
        })
      })
    },
    refresh() {
      this.pageData.pageNum = 1
      this.getList()
    },
    add() {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {}// props
        },
        area: ['1320px', '770px'],
        title: '新增设备信息', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    edit(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            id: row[this.mainId]
          }// props
        },
        area: ['940px', '770px'],
        title: '编辑设备信息', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    deleteOne(row) {
      const _this = this
      this.$confirm('确定要删除吗', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消', cancelButtonClass: 'cancelConfirm',
        type: 'warning'
      }).then(function() {
        remove(row[_this.mainId]).then(res => {
          _this.$message({
            message: res.msg,
            type: 'success'
          })
          _this.getList()
        })
      }).catch(() => {})
    },
    // 当前页的删除接口
    deleteMethod(deleteIds) {
      return remove(deleteIds)
    },
    cancel() {
      this.$layer.close(this.layerid)// 关闭弹框
    },
    onSubmit() {
      // if (Object.keys(this.selectionsRow).length == 0 && !this.warehouseId) {
      //   this.$message.warning('请选择仓库')
      //   return false
      // } else if (Object.keys(this.selectionsRow).length == 0 && this.warehouseId) { // 说明是修改仓库
      //   this.tableData.forEach(item => {
      //     if (item.id == this.warehouseId) {
      //       this.selectionsRow = item
      //     }
      //   })
      // }
      this.$parent.showEquipment(this.selectionsRow)

      this.$layer.close(this.layerid)// 关闭弹框
    },
    tableRowClass({ row }) {
      if (row.bookId == this.userId) {
        return 'rowSelected'
      }
      return ''
    },
    // 单选
    singleElection(row) {
      // console.log(row)
      // this.selectionsRow = row
      // this.templateSelection = row.id
      this.$parent.showEquipment(row)

      this.$layer.close(this.layerid)// 关闭弹框
    }
  }
}
</script>

<style lang="less" scoped>
.main_main_search_input{
  margin-top: 20px;
  margin-bottom: 10px ;
 /deep/ .el-input__inner{
    height: 30px;
  }
}
 /deep/ .el-input__inner{
    height: 30px !important;
  }
.main_main_search_item{
  .el-input{
    width:230px !important;
    margin-right:10px !important;
  }
  .el-button{
    background:#387bfc !important;
    width:30px !important;
  }
}
.table_container{
  margin-top:10px !important;
}
.item_list {
  margin:0 10px 10px 0 !important;
}
.main_main {
  height: calc(100% - 50px) !important;
  margin-right:0 !important;
  padding-top:0 !important;
}
.main_main_search_item{
  height: calc(100% - 50px) !important;
  .el-input{
    width:200px !important;
    margin-right:10px !important;
  }
  .el-button{
    background:#387bfc !important;
    width:30px !important;
  }
}
  .formItem_header_operate_items{
  height: 30px;
  display: flex;justify-content: center;align-items: center;
  padding: 0px 10px;font-size: 16px;border-radius: 4px;
  color: rgba(56, 123, 252, 1);background-color: rgba(235, 242, 255, 1);
  .item_name{
    font-size: 14px;padding-left: 10px;
  }
}
.formItem_header_operate_items:hover{
  background-color: rgba(235, 242, 255, 0.5);
}
.main_main_container {
  width: 100%;
  height: 100%;
  display: flex;

  .main_main_left {
    width: 250px;
    height: calc(100% - 20px);
    padding-right: 20px;
    .title {
      // padding: 10px 0;
      // border-bottom: 1px solid #387BFC;
      margin: 15px 0;
    }
    .main_main_left_top {
      width: 100%;
      height: calc(100% - 80px);
      overflow-y: auto;
      .el-tree {
        background-color: transparent;
      }
      /deep/ .el-tree-node__content:hover {
        background-color: rgba(56, 123, 252, 0.5) !important;
        color: white;
      }
    }
    .main_main_left_bottom {
      width: 100%;
      .main_main_left_bottom_item {
        background-color: rgba(74, 77, 82, 0.2);
        height: 40px;
        display: flex;
        justify-content: flex-start;
        align-items: center;
        padding-left: 24px;
      }
      .main_main_left_bottom_item:last-child {
        border-top: 1px solid white;
      }
      .main_main_left_bottom_item:hover {
        background-color: rgba(56, 123, 252, 0.5);
        color: white;
      }
      .active {
        .main_main_left_bottom_item {
          background-color: rgba(56, 123, 252, 1);
          color: white;
        }
      }
    }
  }
  .main_main_right {
    width: calc(100% - 250px);
    height: 100%;
    display: flex;
    flex-direction: column;
    border-left: 1px solid #ECECEC;

  }
}
.button{
    width: 100px;
    height: 30px;
    color: #fff;
    background-color: #387bfc;
    border-radius: 4px;
    text-align: center;
    line-height: 30px;
}
.formItem_header{
  justify-content: flex-start !important;
  align-items: flex-start !important;
  height: 40px !important;
}
// .formItem_header_operate_items{
//     height: 30px;
//     display: flex;
//     justify-content: center;
//     align-items: center;
//     padding: 0px 10px;
//     font-size: 16px;
//     margin-left: 10px;
//     border-radius: 4px;
//     /* color: #387bfc; */
//     background-color: #ebf2ff;
//     font-family: PingFangSC-Regular;
//     font-size: 14px;
//     font-weight: normal;
//     color: #333333;
//     .item_name{
//       margin-left: 10px;
//     }
// }
</style>
